**NOTE: Change text inside bracets of [folder] to your folder location.
clear
cd "G:\World_Poll\People\Jonathan_Rothwell\WP\WP COVID\Cojucaro_Kim\HSSC replication\"

import delimited "HSSC replication data\Public data\OxCGRT_latest.csv.txt", clear

tostring date, gen(dates)
gen time=date(dates,"YMD")
gen year=year(time)
gen month=month(time)
tab month if year==2021

gen period="Early" if month>=3 & month<=9 & year==2020
replace period="Middle" if month>=10 & year==2020
replace period="Late" if year==2021

*Get regional pop weights
merge m:1 countryname regionname using  "HSSC replication data\Public data\region_weights.dta"
drop if _merge==2
drop _merge
replace region_weight=1 if region_weight==.
**Use aggregation of regions
tab countryname if regioncode!=""
drop if regioncode=="" & (countryname=="Canada" | countryname=="Brazil" | countryname=="United Kingdom" |  countryname=="United States")

tab regionname if countryname=="Canada"
tab regionname if countryname=="Brazil"
tab regionname if countryname=="United Kingdom"
tab regionname if countryname=="United States"

global restrictions "stringencyindex c1_schoolclosing c2_workplaceclosing c3_cancelpublicevents c4_restrictionsongatherings c5_closepublictransport c6_stayathomerequirements c7_restrictionsoninternal c8_internationaltravel h1_publicinformation h2_testingpolicy h3_contacttracing h4_emergency h5_investmentinvaccines h6_facialcoverings h7_vaccinationpolicy h8_protectionofelderlypeople economicsupportindex"

foreach x in $restrictions {
egen Z`x'=std(`x')
}


egen name=concat(countrycode regioncode), punct(-)
encode name, gen(id)
tsset id time

sort id year month
gen obs=1
foreach x in $restrictions {
by id: gen RTM`x'=sum(Z`x')
by id: gen RTN`x'=sum(obs) 
gen RT`x'=RTM`x'/RTN`x'
drop RTM`x' RTN`x'
}

*backwards coding--placebo test
gsort id -year -month
foreach x in $restrictions {
by id: gen FTM`x'=sum(Z`x')
by id: gen FTN`x'=sum(obs) 
gen FT`x'=FTM`x'/FTN`x'
drop FTM`x' FTN`x'
}

tab month if year==2020
tab month if year==2021
*cumulative, January 2020-March 2021

xtile Quin_stringency=RTstringencyindex, nq(5)

collapse (first) regionname countryname (max) time (mean) Quin_stringency FT* RTstringencyindex RTc1_schoolclosing RTc2_workplaceclosing RTc3_cancelpublicevents RTc4_restrictionsongatherings RTc5_closepublictransport RTc6_stayathomerequirements RTc7_restrictionsoninternal RTc8_internationaltravel RTh1_publicinformation RTh2_testingpolicy RTh3_contacttracing RTh4_emergency RTh5_investmentinvaccines RTh6_facialcoverings RTh7_vaccinationpolicy RTh8_protectionofelderlypeople RTeconomicsupportindex ///
[aw=region_weight], by(countrycode month year)

gsort -RTstringencyindex
edit country* RTstringency  if month==3 & year==2021
save "HSSC replication data\Public data\Oxford_Policy_Data_Month_Cumulative_to_Date.dta", replace
